package com.xzc.kafka.action.producer;

import com.xzc.kafka.action.pojo.User;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.Serializer;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class _03InterceptorTest {

    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.put("bootstrap.servers", "es1:19092,es2:19092,es3:19092");
//        props.put("key.serializer", "org.apache.kafka.common.serializtion.StringSerializer");
//        props.put("value.serializer", "org.apache.kafka.common.serializtion.StringSerializer");
        props.put("acks", "-1");
        props.put("retries", 3);
        props.put("batch.size", 323840);
        props.put("linger.ms", 10);
        props.put("buffer.memory", 33554432);
        props.put("max.block.ms", 3000);

        props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, "com.xzc.kafka.action.producer._01AuditPartitioner");

        List<String> interceptors = new ArrayList<>();
        interceptors.add("com.xzc.kafka.action.producer._03TimeStampInterceptor");
        interceptors.add("com.xzc.kafka.action.producer._03TimeStampInterceptor");
        props.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, interceptors);

        Serializer<String> keySerl = new _02Serializer();
        Serializer<String> valSerl = new _02Serializer();
        Producer<String, String> producer = new KafkaProducer<>(props, keySerl, valSerl);

        String topic = "test-topic1";
        for (int i = 0; i < 10; i++) {
            User user = new User("X", "ZC", i, "WUHAN");
            ProducerRecord record = new ProducerRecord(topic, user);
            producer.send(record).get();
        }

        producer.close();
    }
}
